/*
 * Created by JFormDesigner on Sat May 25 23:27:18 CST 2024
 */

package view.user;

import model.booking.Booking;
import model.user.User;

import java.awt.event.*;


import javax.swing.*;

/**
 * 历史记录面板
 */
public class ReservationHistoryPanel extends JPanel {
    private User user;
    public ReservationHistoryPanel(User user) {
        initComponents();
        this.user = user;
        table1.setModel(new ReservationHistoryModel(user));
    }

    //刷新
    private void flush(ActionEvent e) {
        // TODO add your code here
        table1.setModel(new ReservationHistoryModel(user));
    }

    //取消预约
    private void Delete(ActionEvent e) {
        // TODO add your code here
        int row = table1.getSelectedRow();
        if (row == -1)
            JOptionPane.showMessageDialog(this, "请选择要取消的预约");
        else if (JOptionPane.showConfirmDialog(this, "是否取消该预约") == JOptionPane.YES_OPTION) {
            Booking.DeletBookingHostryList(user, row);
            table1.setModel(new ReservationHistoryModel(user));
        }
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        scrollPane1 = new JScrollPane();
        table1 = new JTable();
        fulshButton = new JButton();
        DeletButton = new JButton();

        //======== this ========

        //======== scrollPane1 ========
        {
            scrollPane1.setViewportView(table1);
        }

        //---- fulshButton ----
        fulshButton.setText("\u5237\u65b0");
        fulshButton.addActionListener(e -> flush(e));

        //---- DeletButton ----
        DeletButton.setText("\u53d6\u6d88\u9884\u7ea6");
        DeletButton.addActionListener(e -> Delete(e));

        GroupLayout layout = new GroupLayout(this);
        setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup()
                .addComponent(scrollPane1, GroupLayout.DEFAULT_SIZE, 495, Short.MAX_VALUE)
                .addGroup(GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                    .addContainerGap(288, Short.MAX_VALUE)
                    .addComponent(fulshButton)
                    .addGap(38, 38, 38)
                    .addComponent(DeletButton)
                    .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup()
                .addGroup(layout.createSequentialGroup()
                    .addComponent(scrollPane1, GroupLayout.PREFERRED_SIZE, 301, GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                        .addComponent(DeletButton)
                        .addComponent(fulshButton)))
        );
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    private JScrollPane scrollPane1;
    private JTable table1;
    private JButton fulshButton;
    private JButton DeletButton;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on


    /*
    根据用户身份显示不同的历史记录
     */
    public void showHistory(String identity)
    {

    }

}
